<template>
	<view>
		<navbar name="记录心率"></navbar>
		<view class="weui-flex">
			<view>
				<view class="placeholder titleView">
					心率
				</view>
			</view>
			<view class="weui-flex__item">
				<view class="placeholder toolView" :class="label2 == '请选择心率'?'':'colorA4'"
					@click="handleTap('picker2')">
					<!-- {{value2.join('')}} -->
					{{label2}}
				</view>
			</view>
		</view>
		<u-line />
		<view class="weui-flex">
			<view>
				<view class="placeholder titleView">
					选择时间
				</view>
			</view>
			<view class="weui-flex__item">
				<view class="placeholder toolView" :class="value1==''?'':'colorA4'" @click="handleTap('picker1')">
					{{value1==''?label:value1.replace(/-/g,".")}}
				</view>
			</view>
		</view>
		<button class="relevance-btn" type="default" :disabled="disabled||label2 == '请选择心率'||value1==''?true:false"
			:class="label2 == '请选择心率'||value1==''?'disabledbg':''" @tap="addRecord(value2,value1)">保存</button>
		<!-- 日期选择 -->
		<lb-picker ref="picker1" v-model="value1" :format="curFormat" display-format="YYYY-MM-DD-HH-mm-ss"
			mode="dateSelector" @change="handleChange" @confirm="handleConfirmTime" @cancel="handleCancel"
			start-date="2021-1-1" :end-date="endTime">
		</lb-picker>
		<!-- end -->
		<!-- 血压选择 -->
		<!-- <lb-picker ref="picker2" v-model="value2" mode="multiSelector" :list="list2" :level="2"
			:dataset="{ name: 'label2' }" @change="handleChange" @confirm="handleConfirm" @cancel="handleCancel">
		</lb-picker> -->
		<lb-picker ref="picker2" v-model="value2" mode="unlinkedSelector" :list="list2" @change="handleChange"
			@confirm="handleConfirm" @cancel="handleCancel">
		</lb-picker>
		<!-- end -->
	</view>
</template>

<script>
	import dayjs from '@/common/utils/day/day.js'
	import lbPicker  from '@/pages/subpackage/components/lb-picker/index';

import uLine from "@/uview-ui/components/u-line/u-line.vue"

	export default {
		components: {
			lbPicker,
			uLine,
		},
		data() {
			return {
				valueExpressionType:0,
				listLength: {},
				curFormat: 'YYYY-MM-DD HH:mm:ss',
				disabled: false,
				endTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
				indicId: '', //指标id
				label2: '请选择心率',
				value2: [
					85,
					'次/分',
				],
				value1: '',
				label: '请选择时间',
				list2: [

				]
			}
		},
		onLoad(e) {
			this.indicId = e.indicId ? e.indicId : 3;
			this.getRangeIndicator()
		},
		methods: {
			handleConfirmTime(){

			},
			listFun(min,max){
				let list2 = this.list2,
					list2Arr = [];
				for (let i = min; i < max; i++) {
					list2Arr.push({
						label: i,
						value: i
					})
				}
				this.$set(this.list2,2,list2Arr)
			},
			//测量指标范围列表
			getRangeIndicator() {
				this.$api.get(global.apiUrls.getRangeIndicator, {})
					.then(res => {
						let result = res.data.result,
							range,
							type;
						for (let i = 0; i < result.length; i++) {
							if ('HEART_RATE' == result[i].itemCode) { //血压
								range = result[i].valueExpression.range
								type = result[i].valueExpression.type
							}
						}
						this.valueExpressionType = type
						this.$basejs.funFor(range, this, 'list2','次/分')
						// let num1 = parseFloat(range.split('-')[0].replace('(', '')),
						// 	num2 = parseFloat(range.split('-')[1]);
						// 	this.listFun(num1,num2)
						// this.listFun(xsList[0], xsList[1], xsList[2], xsList[3])
						console.log(range, 12222)

					})
			},
			handleConfirmTime(e) { //时间回调

			},
			// 记录心率
			addRecord(blood, time) {
				this.disabled = true
				let _this = this,
					fdata = {
						deviceType: 'HEART_RATE',
						recordTime: dayjs(time).format('YYYY-MM-DD'),
						createTime: time,
						inputType: 1, //1手动 2设备
						userId: uni.getStorageSync(global.IS_DEV?'USER_DATA_BSY_T':'USER_DATA_BSY_Z').userId,
						mac: '', //设备mac
						deviceId: '', //设备id
						healthDataId: '',
						healthData: JSON.stringify({
							heartRateValue: this.$basejs.itemFor(this, 'list2', blood, false,this.valueExpressionType)
						}),
					};
				// console.log(blood,fdata)
				this.$api.post(global.apiUrls.addRecord, fdata).then(res => {
					if (res.data.code == 1000) {
						uni.showToast({
							title: res.data.message,
							duration: 2000
						});
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							});
						}, 2000);
						_this.value1 = '';
						_this.label2 = '请选择心率'
					} else {
						uni.showToast({
							title: res.data.message,
							icon: "none",
							duration: 2000
						});
					}
					_this.disabled = false;
				}).catch(err => {
					_this.disabled = false;
				})
			},
			handleTap(name) {
				this.$refs[name].show()
			},
			handleChange(e) {
				console.log('change::', e)
			},
			handleConfirm(e) {
				console.log(e)
				this.label2 = this.$basejs.itemFor(this, 'list2', e.item, true,this.valueExpressionType)
			},
			handleCancel(e) {
				console.log('cancel::', e)
			},
		}
	}
</script>

<style lang="scss">
	@import "./record.scss"
</style>
